// config.js
let baseUrl = "https://newcoss-dev.isignet.cn:10201/coss";
let appId = "APP_D45364B75AC649A49A09CE9FCF16ADDC";
let secret = "NTk3MGI1MzkyMGEwNDQ1OWFkZDM1NTA0ZjFhZDA4NWY=";
let signature = "";

function calculateSignature() {
    // 调用 abc 方法，假设 abc 方法返回一个加密字符串
    const encryptedString = abc();
    return encryptedString;
}
function signature(){
    // 计算报文签名值
    var data;
    var headers = JSON.parse(JSON.stringify(request.headers));
    console.log(headers)
    if(headers['content-type'] === "application/json"){
        console.log("请求类型为json")
        data = JSON.parse(pm.environment.replaceIn(request.data));
    }else{
        data=JSON.parse(JSON.stringify(request.data))

    }
    var secret = pm.environment.get("secret");
    console.log("key:"+secret)
    var signature = calcSign(data,secret);
    console.log(signature!="" ? signature:"无")
    pm.environment.set("signature",signature)
    function calcSign(data,secret){
        delete data['signature']
        var keys = [];
        for(var k in data){
            keys.push(k);
        }
        keys.sort();
        var kv=[];
        for(var v of keys){
            if(data[v] === null || data[v] === ""){
                continue;
            }
            kv.push(v+'='+data[v]);
        }
        var kvStr = kv.join("&");
        return CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(kvStr,secret));
    }
}
function updateSignature() {
    signature = calculateSignature();
}

module.exports = {
    baseUrl,
    appId,
    secret,
    signature,
    updateSignature
};
